package ARKit;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.location.Location;
import android.preference.PreferenceManager;
import android.view.View;

public class ARSphericalView extends View 
{
	public volatile float azimuth; //Angle from north
	public volatile float distance; //Distance to object
	public volatile float inclination = -1; //angle off horizon.
	public volatile Location location;
	
	public volatile int x;
	public volatile int y;
	public volatile boolean visible = true;
	
	public static Location deviceLocation;
	//used to compute inclination
	public static float currentAltitude = 0;
	protected Paint p = new Paint();
	private float correction = 0.0f;
	private SharedPreferences pref;
	
	public ARSphericalView(Context ctx)
	{
		super(ctx);
	}
	public ARSphericalView(Context ctx, Location deviceLocation, Location objectLocation)
	{
		super(ctx);
		pref = PreferenceManager.getDefaultSharedPreferences(ctx);
		correction = Float.parseFloat( pref.getString("aimuth_correction", "0"));
		if(deviceLocation != null)
		{
			azimuth = deviceLocation.bearingTo(objectLocation);
			distance = deviceLocation.distanceTo(objectLocation);
			if(deviceLocation.hasAccuracy() && objectLocation.hasAltitude())
			{
				azimuth = deviceLocation.bearingTo(objectLocation);
				distance = deviceLocation.distanceTo(objectLocation);
				if(deviceLocation.hasAccuracy() && objectLocation.hasAltitude())
				{
					double opposite;
					boolean neg = false;
					if(objectLocation.getAltitude() > deviceLocation.getAltitude())
					{
						opposite = objectLocation.getAltitude() - deviceLocation.getAltitude();
					}
					else
					{
						opposite = deviceLocation.getAltitude() - objectLocation.getAltitude();
						neg = true;
					}
					inclination = (float) Math.atan(((double)opposite/distance));
					if(neg)
						inclination = inclination * -1;
				}
				
				
			}
			azimuth += correction;
			azimuth = azimuth % 361;
		}
	}
	@Override
	public void draw(Canvas c)
	{

		
	}

	
}
